#include<mainwindow.h>
#include<maplabel.h>
MapLabel::MapLabel(const QString &text, QWidget *parent,int width,int height) : QLabel(text,parent){

    mousestatus=NONE;
    w=width;
    h=height;

    this->setGeometry(0,0,w*24,h*24);
    this->setFixedSize(w*24,h*24);
    this->setText("");

    shutevent=false;

    mapl=new QLabel(this);
    mapl->setFixedSize(this->size());
    bgl=new QLabel(this);
    bgl->setFixedSize(this->size());
    bgl->setAlignment(Qt::AlignCenter);


    init();

}

void MapLabel::mousePressEvent(QMouseEvent *event){
    if(shutevent) return;
    aa=event->pos().y()/24+1;
    bb=event->pos().x()/24+1;
    if(event->button()==Qt::LeftButton) {
        if(painterstatus==0){
            switch(status[aa][bb]){
            case 2:
                start=QPoint();
                break;
            case 3:
                end=QPoint();
                break;
            }

            status[aa][bb]=1;
            paintnow(aa,bb,1);
            mousestatus=LEFT;
        }
        else if(painterstatus==2){
            setstartpoint(aa,bb);
        }
        else if(painterstatus==3){
            setendpoint(aa,bb);
        }
    }
    if(event->button()==Qt::RightButton) {
        switch(status[aa][bb]){
        case 2:
            start=QPoint();
            break;
        case 3:
            end=QPoint();
            break;
        }

        status[aa][bb]=0;
        paintnow(aa,bb,0);
        mousestatus=RIGHT;
    }
}
void MapLabel::mouseMoveEvent(QMouseEvent *event){
    if(shutevent) return;
    aa=event->pos().y()/24+1;
    bb=event->pos().x()/24+1;
    if(aa<=0 or aa>h or bb<=0 or bb>w) return;
    if(mousestatus==LEFT and painterstatus==0) {
        if(status[aa][bb]==0){
            status[aa][bb]=1;
            paintnow(aa,bb,1);

        }
    }
    if(mousestatus==RIGHT) {
        if(status[aa][bb]!=0){
            switch(status[aa][bb]){
            case 2:
                start=QPoint();
                break;
            case 3:
                end=QPoint();
                break;
            }
            status[aa][bb]=0;
            paintnow(aa,bb,0);
        }
    }

}
void MapLabel::mouseReleaseEvent(QMouseEvent *event){
    mousestatus=NONE;
}

//need to be edit
void MapLabel::setMapPath(){

    nullpath.clear();
    brickspath.clear();
    waypath.clear();

    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            if(status[i][j]==0) nullpath.addRoundedRect(2+(j-1)*24,2+(i-1)*24,20,20,6,6);
            if(status[i][j]==1) brickspath.addRoundedRect(2+(j-1)*24,2+(i-1)*24,20,20,6,6);
            if(status[i][j]==4) waypath.addRoundedRect(2+(j-1)*24,2+(i-1)*24,20,20,6,6);
        }
    }

    paintnow();
}


void MapLabel::init(){
    painterstatus=0;
    mappic=QPixmap(this->size());

    nullpath.clear();
    brickspath.clear();
    waypath.clear();
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            status[i][j]=0;
            nullpath.addRoundedRect(2+(j-1)*24,2+(i-1)*24,20,20,6,6);
        }
    }
    paintnow();

}
void MapLabel::paintnow(){

    mappic.fill(Qt::transparent);

    QPainter painter(&mappic);

    painter.setPen(Qt::black);
    painter.setRenderHint(QPainter::Antialiasing);

    //painter.setBrush(QBrush(Qt::white));
    painter.setBrush(QBrush(QColor(255,255,255,60)));
    painter.drawPath(nullpath);
    painter.setBrush(QBrush(Qt::blue));
    painter.drawPath(brickspath);
    painter.setBrush(QBrush(Qt::yellow));
    painter.drawPath(waypath);
    painter.end();

    if(!start.isNull()) paintnow(start.x(),start.y(),2);
    if(!end.isNull()) paintnow(end.x(),end.y(),3);

    mapl->setPixmap(mappic);
}
void MapLabel::paintnow(int x,int y,int sta,bool isUpdatenow){
    QPainter painter(&mappic);

    painter.setPen(Qt::black);
    painter.setRenderHint(QPainter::Antialiasing);



    switch(sta){
    case 0:

        painter.setCompositionMode(QPainter::CompositionMode_Clear);
        painter.eraseRect((y-1)*24,(x-1)*24,24,24);

        painter.setCompositionMode(QPainter::CompositionMode());
        painter.setBrush(QBrush(QColor(255,255,255,60)));
        painter.drawRoundedRect(2+(y-1)*24,2+(x-1)*24,20,20,6,6);
        break;
    case 1:

        painter.setCompositionMode(QPainter::CompositionMode_Clear);
        painter.eraseRect((y-1)*24,(x-1)*24,24,24);

        painter.setBrush(QBrush(Qt::blue));
        painter.setCompositionMode(QPainter::CompositionMode());
        painter.drawRoundedRect(2+(y-1)*24,2+(x-1)*24,20,20,6,6);
        break;
    case 2:

        painter.setCompositionMode(QPainter::CompositionMode_Clear);
        painter.eraseRect((y-1)*24,(x-1)*24,24,24);

        painter.setBrush(QBrush(Qt::red));
        painter.setCompositionMode(QPainter::CompositionMode());
        painter.drawRoundedRect(2+(y-1)*24,2+(x-1)*24,20,20,6,6);
        break;
    case 3:

        painter.setCompositionMode(QPainter::CompositionMode_Clear);
        painter.eraseRect((y-1)*24,(x-1)*24,24,24);

        painter.setBrush(QBrush(Qt::green));
        painter.setCompositionMode(QPainter::CompositionMode());
        painter.drawRoundedRect(2+(y-1)*24,2+(x-1)*24,20,20,6,6);
        break;
    case 4:

        painter.setCompositionMode(QPainter::CompositionMode_Clear);
        painter.eraseRect((y-1)*24,(x-1)*24,24,24);

        painter.setBrush(QBrush(Qt::yellow));
        painter.setCompositionMode(QPainter::CompositionMode());
        painter.drawRoundedRect(2+(y-1)*24,2+(x-1)*24,20,20,6,6);
        break;
    }

    painter.end();
    if(isUpdatenow) mapl->setPixmap(mappic);

}
void MapLabel::setstartpoint(int x, int y){
    if(!start.isNull() and status[start.x()][start.y()]==2){
        paintnow(start.x(),start.y(),0);
        status[start.x()][start.y()]=0;
    }
    start.setX(x);
    start.setY(y);
    status[x][y]=2;
    paintnow(x,y,2);
}
void MapLabel::setendpoint(int x, int y){
    if(!end.isNull() and status[end.x()][end.y()]==3){
        paintnow(end.x(),end.y(),0);
        status[end.x()][end.y()]=0;
    }
    end.setX(x);
    end.setY(y);
    status[x][y]=3;
    paintnow(x,y,3);
}
void MapLabel::setpainterstatus(int sta){
    painterstatus=sta;
}
void MapLabel::setbackground(QImage bg){
    backpic=QImage(bg.scaled(this->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));

    bgl->clear();
    bgl->setFixedSize(this->size());
    bgl->setPixmap(QPixmap::fromImage(backpic));
    bgl->stackUnder(mapl);
    bgl->show();
}
void MapLabel::clearbackground(){
    bgl->clear();
}
